// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "CoreTypes.h"
#include "Containers/Array.h"
#include "Delegates/Delegate.h"
#include "GameProjectHelper.h"
#include "ILauncher.h"
#include "ILauncherProfileManager.h"
#include "Misc/Paths.h"
#include "Misc/ConfigCacheIni.h"
#include "Misc/App.h"
#include "Templates/SharedPointer.h"
//#include "Profiles/LauncherProfile.h"


class FHotPakerModel;


/**
 * Implements the data model for the session launcher.
 */
class FHotPakerModel
{
public:

	/**
	 * Creates and initializes a new instance.
	 *
	 * @param InDeviceProxyManager The device proxy manager to use.
	 * @param InLauncher The launcher to use.
	 * @param InProfileManager The profile manager to use.
	 */
	FHotPakerModel(const TSharedRef<ILauncherProfileManager>& InProfileManager) : ProfileManager(InProfileManager)
	{
		//HotPakerProfile = MakeShareable(new FLauncherProfile(InProfileManager));
		HotPakerProfile = InProfileManager->AddNewProfile();
		ProfileManager->RemoveProfile(HotPakerProfile.ToSharedRef());
		//HotPakerProfile = MakeShareable(new FLauncherProfile(InProfileManager, FGuid::NewGuid(), TEXT("HotPakerProfile")));

		LoadConfig();
	}

	/** Destructor. */
	~FHotPakerModel()
	{
		//ProfileManager->RemoveProfile(HotPakerProfile.ToSharedRef());

		SaveConfig();
	}

	const TSharedPtr<ILauncherProfile>& GetHotPakerProfile() const
	{
		return HotPakerProfile;
	}

protected:

	/*
	 * Load all profiles from disk.
	 *
	 * @see SaveConfig
	 */
	void LoadConfig()
	{
		// restore the previous project selection
		FString ProjectPath;

		if (FPaths::IsProjectFilePathSet())
		{
			ProjectPath = FPaths::GetProjectFilePath();
		}
		else if (FGameProjectHelper::IsGameAvailable(FApp::GetProjectName()))
		{
			ProjectPath = FPaths::RootDir() / FApp::GetProjectName() / FApp::GetProjectName() + TEXT(".uproject");
		}
		else if (GConfig != nullptr)
		{
			GConfig->GetString(TEXT("FHotPakerModel"), TEXT("SelectedProjectPath"), ProjectPath, GEngineIni);
		}

	}

	/*
	 * Saves all profiles to disk.

	 * @see LoadConfig
	 */
	void SaveConfig()
	{
		if (GConfig != nullptr && !FPaths::IsProjectFilePathSet() && !FGameProjectHelper::IsGameAvailable(FApp::GetProjectName()))
		{
			//GConfig->SetString(TEXT("FHotPakerModel"), TEXT("SelectedProjectPath"), *ProjectPath, GEngineIni);
		}
	}

private:

	TSharedPtr<ILauncherProfile> HotPakerProfile;

	/** Pointer to the profile manager. */
	TSharedRef<ILauncherProfileManager> ProfileManager;

};
